package org.example.servletpractice.mapper;

import org.example.servletpractice.pojo.Websites;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class WebsitesMapper {

    public List<Websites> getWebsites() throws SQLException {
        //获取连接
        Connection connection = JDBC.getConnection();
        //sql语句
        String sql = "SELECT * FROM websites";
        //创建PreparedStatement
        PreparedStatement prepared = connection.prepareStatement(sql);
        //执行sql
        ResultSet resultSet = prepared.executeQuery();
        //获取数据
        List<Websites> websitesList = new ArrayList<>();
        while (resultSet.next()){
            Websites websites=new Websites();
            websites.setId(resultSet.getInt("id"));
            websites.setName(resultSet.getString("name"));
            websites.setUrl(resultSet.getString("url"));
            websites.setAlexa(resultSet.getInt("alexa"));
            websites.setCountry(resultSet.getString("country"));
            websitesList.add(websites);
        }
        JDBC.close(connection,null,prepared,resultSet);
        return websitesList;
    }

}
